package com.book.remote;

import com.book.dto.BookDto;
import com.book.dto.ShopCarDto;
import com.book.dto.ShopDetailDto;
import com.book.service.BookService;
import com.book.service.ImgService;
import com.book.service.ShopCarDetailService;
import com.book.service.ShopCarService;
import com.book.util.Constants;
import com.book.vo.ShopCarVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author 李锦程
 * @Date 2021/4/22 6:57 下午
 * @Package com.book.service
 * @Version 1.0
 */
@Component
@Slf4j
public class ShopCarRemote {
    @Autowired
    private ShopCarService shopCarService;
    @Autowired
    private ShopCarDetailService shopCarDetailService;
    @Autowired
    private BookService uBookService;
    @Autowired
    private ImgService imgService;

    public ShopCarDto getCarByUId(Integer uId) {
        return shopCarService.getCarByUId(uId);
    }

    public List<ShopCarVo> getShopCarDetail(String scCode) {
        try {
            List<ShopDetailDto> shopCarDetail = shopCarDetailService.getShopCarDetail(scCode);
            if (CollectionUtils.isEmpty(shopCarDetail)) {
                return Lists.newArrayList();
            }
            List<ShopCarVo> voList = Lists.newArrayList();
            shopCarDetail.forEach(m -> {
                ShopCarVo vo = new ShopCarVo();
                vo.setShopDetailDto(m);
                BookDto uBookDto = uBookService.getUBookByCode(m.getUbCode());
                vo.setUBookDto(uBookDto);
                List<String> imgByBookCode = imgService.getImgByBookCode(uBookDto.getBookCode());
                if (CollectionUtils.isNotEmpty(imgByBookCode)){
                    vo.setImgUrl(imgByBookCode.get(0));
                }
                voList.add(vo);
            });
            return voList;
        } catch (Exception e) {
            return Lists.newArrayList();
        }

    }

    public void addCar(ShopCarDto carDto) {
        if (carDto == null) {
            return;
        }
        shopCarService.insertShopCar(carDto);
    }

    public String addBookToCar(String ubCode, String uCode) {
        BookDto uBookByCode = uBookService.getUBookByCode(ubCode);
        log.info("ShopCarRemote.AddBookToCar book:{}",uBookByCode);
        if (uBookByCode == null) {
            return Constants.ERROR;
        }
        ShopDetailDto shopDetailDto = new ShopDetailDto();
        shopDetailDto.setScCode(uCode);
        shopDetailDto.setSdCreateTime(new Date());
        shopDetailDto.setSdNum((short) 1);
        shopDetailDto.setSdStatus((short) 1);
        shopDetailDto.setUbCode(ubCode);
        shopDetailDto.setPrice(uBookByCode.getBookPrice());
        Integer i = shopCarDetailService.save(shopDetailDto);
        return i == 1 ? Constants.SUCCESS : Constants.ERROR;
    }

    public void updateCarBookNum(Integer id, int num) {
        shopCarDetailService.updateCarBookNum(id, num);
    }

    public Integer deleteCarBook(Integer id) {
        return shopCarDetailService.deleteCarBook(id);
    }


    public void deleteShopCarDetail(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return;
        }
        ids.forEach(m->{
            shopCarDetailService.deleteCarBook(m);
        });
    }
}
